package com.english.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * 配置验证器 - 启动时检查配置是否正确
 */
@Slf4j
@Component
public class ConfigurationValidator implements CommandLineRunner {

    @Autowired
    private WechatPayConfig wechatPayConfig;

    @Override
    public void run(String... args) throws Exception {
        log.info("=== WeChat Pay 配置检查 ===");
        log.info("AppId: {}", wechatPayConfig.getAppId());
        log.info("商户号: {}", wechatPayConfig.getMchId());
        log.info("回调地址: {}", wechatPayConfig.getNotifyUrl());
        log.info("统一下单URL: {}", wechatPayConfig.getUnifiedOrderUrl());
        
        // 检查API Key格式
        String apiKey = wechatPayConfig.getApiKey();
        if (apiKey == null || apiKey.isEmpty()) {
            log.error("❌ API密钥未配置！");
        } else if ("YOUR_32_CHARACTER_WECHAT_PAY_API_KEY_HERE".equals(apiKey)) {
            log.warn("⚠️  请替换为真实的WeChat Pay API密钥");
        } else if (apiKey.length() != 32) {
            log.error("❌ API密钥长度错误！当前长度: {}, 应该是32位", apiKey.length());
        } else if (!apiKey.matches("^[a-zA-Z0-9]+$")) {
            log.error("❌ API密钥格式错误！应该只包含字母和数字");
        } else {
            log.info("✅ API密钥格式正确 (长度: {})", apiKey.length());
        }
        
        log.info("===============================");
    }
} 